package com.genesis.yunnanji.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.config.GenesisApiConfig;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import es.dmoral.toasty.Toasty;



public class ChangePwd extends BaseActivity {
    @ViewInject(R.id.tb_changepwd_toolbar)
    private Toolbar mToolbar;
    @ViewInject(R.id.tv_changepwd_title)
    private TextView tvTitle;
    @ViewInject(R.id.et_changepwd_before)
    private EditText etBefore;
    @ViewInject(R.id.et_changepwd_new)
    private EditText etNew;
    @ViewInject(R.id.et_changepwd_again)
    private EditText etRep;
    @ViewInject(R.id.bt_changepwd_comfirm)
    private Button btComfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_changpwd);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (getIntent().getIntExtra("pwd_status", -1) == 0) {
            tvTitle.setText("设置密码");
            etBefore.setVisibility(View.GONE);
        } else if (getIntent().getIntExtra("pwd_status", -1) == 1) {
            tvTitle.setText("修改密码");
            etBefore.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void setUpView() {
        btComfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(etBefore.getText()) && !TextUtils.isEmpty(etNew.getText()) && !TextUtils.isEmpty(etRep.getText())) {
                    changePwd();
                } else {
                    Toasty.info(context, "请先完善资料！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void changePwd() {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.CHANGE_PWD);
        params.addBodyParameter("__apptoken__", genesisUtils.getAppToken());
        if (getIntent().getIntExtra("pwd_status", -1) == 1) {
            params.addBodyParameter("uoldpwd", genesisUtils.getMD5Str("jzwhcb" + genesisUtils.getMD5Str(etBefore.getText().toString())));
        }
        params.addBodyParameter("upwd", etNew.getText().toString());
        params.addBodyParameter("urpwd", etRep.getText().toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optInt("status") == 200) {
                        genesisUtils.setUserImage(jsonObject.getJSONObject("result").optString("issetpwd"));
                        Toasty.success(context, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toasty.error(context, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
